package cn.flashk.controls.skin.sourceSkin {
	import com.skins.Skin;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import cn.flashk.controls.Button;
	import cn.flashk.controls.ComboBox;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SourceSkinLinkDefine;
	import cn.flashk.controls.support.UIComponent;

	public class ComboBoxSourceSkin extends SourceSkin {
		private static var skin:DisplayObject;
		private static var comboBg:Button;
		private static var bd:BitmapData;
		private static var bds:Array;
		private var tar:ComboBox;
		private var sp:Sprite; //背景
		private var arrow:DisplayObject;

		public function ComboBoxSourceSkin(skinID:String=""){
			super(skinID);
			sp = new Sprite();
			if(_skinID==""){
				sp.addChild(bp);
			}
		}

		override public function init(target:UIComponent, styleSet:Object, Skin:Class):void {
			super.init(target, styleSet, Skin);
			tar = target as ComboBox;
			if (Skin == null){
				comboBg = new Button(library.sheet.getStyleValue(_skinID,"comboBg"))
				if(comboBg){
					sp.addChildAt(comboBg, 0);
				}
				arrow = library.getSkin(_skinID,"arrow");
				if(arrow){
					comboBg.addChild(arrow);
				}
				tar.addChildAt(sp, 0);
				return;
			}
			
			if (skin == null){
				skin = new Skin() as DisplayObject;
			}
			initBp(skin);
			var mc:MovieClip = skin as MovieClip;
			if (bds == null){
				bds = new Array();
				drawMovieClipToArray(skin as MovieClip, bds);
			}
			bp.sourceBitmapData = bds[1] as BitmapData;
			tar.addChildAt(sp, 0);
			tar.addEventListener(MouseEvent.MOUSE_OVER, showOver);
			tar.addEventListener(MouseEvent.MOUSE_OUT, showOut);
			tar.addEventListener(MouseEvent.MOUSE_DOWN, showPress);
			tar.addEventListener(MouseEvent.MOUSE_UP, showUp);
			var ClassRef:Class = SkinLoader.getClassFromSkinFile(SourceSkinLinkDefine.COMBO_BOX_ARROW);
			arrow = new ClassRef() as DisplayObject;
			sp.addChild(arrow);
		}

		public function get skinDisplayObject():DisplayObject {
			return sp;
		}

		protected function showUp(event:MouseEvent):void {
			bp.sourceBitmapData = bds[1] as BitmapData;
			bp.update();
		}

		protected function showPress(event:MouseEvent):void {
			bp.sourceBitmapData = bds[3] as BitmapData;
			bp.update();
		}

		protected function showOut(event:MouseEvent):void {
			bp.sourceBitmapData = bds[1] as BitmapData;
			bp.update();
		}

		protected function showOver(event:MouseEvent):void {
			bp.sourceBitmapData = bds[2] as BitmapData;
			bp.update();
		}

		override public function reDraw():void {
			if (_skinID != ""){
				comboBg.width = tar.compoWidth;
				comboBg.height = tar.compoHeight;
				arrow.x = comboBg.width - arrow.width - 2;
				arrow.y = int((comboBg.height - arrow.height) / 2);
				return;
			}
			bp.width = tar.compoWidth + sx * 2;
			bp.height = tar.compoHeight + sy * 2;
			arrow.x = bp.width - arrow.width - 2;
			arrow.y = int((bp.height - arrow.height) / 2);
		}
	}
}